package flatlandRPG;

import java.util.LinkedList;

// -------------------------------------------------------------------------
/**
 * Manages collisions between movable objects and solid objects. If an object
 * implements both IMoveable and ISolid, you will have to add it twice, casting
 * it to each type.
 *
 * @author evant
 * @version Apr 17, 2011
 */
public class CollisionManager
    implements ILogic
{
    private LinkedList<IMoveable> moveables;
    private LinkedList<ISolid>    solids;


    // ----------------------------------------------------------
    /**
     * Create a new CollisionManager object.
     */
    public CollisionManager()
    {
        moveables = new LinkedList<IMoveable>();
        solids = new LinkedList<ISolid>();
    }


    // ----------------------------------------------------------
    /**
     * Adds a movable object to the collision manager.
     *
     * @param object
     */
    public void add(IMoveable object)
    {
        moveables.add(object);
    }


    // ----------------------------------------------------------
    /**
     * Adds a solid object to the collision manager.
     *
     * @param object
     */
    public void add(ISolid object)
    {
        solids.add(object);
    }


    /**
     * Detect collision between the added solids and moveables. If there is
     * collision, let the movable handle it
     */
    public void doLogic()
    {
        for (ISolid solid : solids)
        {
            for (IMoveable moveable : moveables)
            {
                if (solid.collidedWith(moveable) && solid != moveable)
                {
                    moveable.collision(solid);
                }
            }
        }

    }
}
